package view;

import game.*;

import javax.swing.*;

/**
 * <p>A GUI to show a game of chess. It also allows the user to select his/her desired variant of chess, as well as displaying help.</p>
 * 
 * @author Nicole Waldrum
 * @date January 30, 2010
 * @version 1.0
 * 
 * Added the basic GUI so that the player can choose the game that they would like to implement.
 * 
 * @author Nicole Waldrum
 * @date February 19, 2010
 * @version 2.0
 *
 * @author Trevor Bastian
 * @date February 21, 2010
 * @version 4.0
 * @version 3.0
 * @version 2.0
 * 
 * @author Mark Kelly
 * @date February 21, 2010
 * @version 2.0
 *
 * @author Mark Kelly
 * @date March 6, 2010
 * @version 3.0
 */
public class ChessGUI extends ChessView {
	private JFrame mainFrame;
	private JMenuBar menuBar;
	private GUIBridge gb;
	private BoardPanel board;

	/**
	 * <p>Creates a new <tt>ChessGUI</tt>.</p>
	 */
	public ChessGUI(GUIBridge gb) {		
		this.gb = gb;		
		initComponents();
	}
	
	/**
	 * <p>Initialise all GUI components</p>
	 */
	private void initComponents() {
		mainFrame = new JFrame("Intellectual Thinking Game");
		mainFrame.setSize(500,580);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		menuBar = new JMenuBar();
		JMenu tempMenu;
		JMenuItem tempMenuItem;

		// File Menu
		tempMenu = new JMenu("File");
		
		tempMenuItem = new JMenuItem("Save");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("Load");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);

		tempMenu.add(new JSeparator());
		
		tempMenuItem = new JMenuItem("Exit");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		// Edit Menu
		tempMenu = new JMenu("Edit");
		
		tempMenuItem = new JMenuItem("Undo");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("Redo");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		// Game Type Menu
		tempMenu = new JMenu("Game Type");
		
		tempMenuItem = new JMenuItem("Chess");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("Knight's Tour");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("8 Queen's Problem");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		// Help Menu
		tempMenu = new JMenu("Help");
		
		tempMenuItem = new JMenuItem("Help");
		tempMenuItem.addActionListener(gb);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		mainFrame.setJMenuBar(menuBar);
		
		// The text display area
		board = new BoardPanel ();
		board.addActionListener(gb);

		mainFrame.add(board);
		
		mainFrame.setVisible(true);
	}

	/**
	 * <p>Redraw the <tt>ChessBoard</tt> and every <tt>Piece</tt>.</p>
	 */
	private void redraw (ChessBoard b) {
		board.refresh(b);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void pieceMoved(GameState gs, Piece p, ChessBoard b) {
		redraw(b);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void showHelp(GameType type) {
		new HelpGUI(type);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void gameWon(Team team) {
		showMessage("Game Over", team.toString() + " wins!");		
	}
	
	@Override
	public void gameTied(String reason) {
		showMessage("Tie", reason);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void gameStarted(GameState gs) {
		redraw(gs.getBoard());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void stateChanged(GameState gs) {
		redraw(gs.getBoard());		
	}
	
	/**
	 * {@inheritDoc}
	 */

	@Override
	public void showMessage(String message) {
		showMessage("Message", message);
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @param title
	 * @param message
	 */
	private void showMessage(String title, String message) {
		JOptionPane.showMessageDialog(null, message, title, JOptionPane.OK_OPTION);
	}

	@Override
	public void needPieceChoice(GameState gs) {
	}
}
